package org.example.easybuy.controller;

import com.github.pagehelper.PageInfo;
import org.example.easybuy.entity.Category;
import org.example.easybuy.service.CategoryService;
import org.example.easybuy.util.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/getCategoryPage")
    public CustomResult getCategoryPage(@RequestParam Map<String,Object> params)
    {
        PageInfo<Category> pageInfo=categoryService.getCategoryPageInfo(params);
        return CustomResult.successResult().addResult("pageInfo",pageInfo);
    }

    @GetMapping("/getCategoryList")
    public CustomResult getCategoryList(int type)
    {
        return CustomResult.successResult().addResult("cateList",categoryService.selectByType(type));
    }

    @GetMapping("/getCategoryListInThis")
    public CustomResult getCategoryListInThis(int id)
    {
        return CustomResult.successResult().addResult("cateList",categoryService.selectListById(id));
    }

    @GetMapping("/checkCateName")
    public CustomResult checkCateName(@RequestParam Map<String,Object> params)
    {
        if(params.get("name").equals(""))
        {
            return CustomResult.successResult().addResult("isExist","null");
        }
        Category category=categoryService.checkCateName(params);
        if(category==null)
        {
            return CustomResult.successResult().addResult("isExist","no");
        }
        return CustomResult.successResult().addResult("isExist","yes");
    }


    @GetMapping("/addCategory")
    public CustomResult addCategory(@RequestParam Map<String,Object> params)
    {
        if(categoryService.addCategory(params))
        {
            return CustomResult.successResult().addResult("isAdd","yes");
        }else{
            return CustomResult.errorResult(500,"添加出错了").addResult("isAdd","no");
        }
    }


    @GetMapping("/getUpdateCategory")
    public CustomResult getUpdateCategory(String id,int cateType)
    {
        return CustomResult.successResult().addResult("category",categoryService.getUpdateCategory(id,cateType));
    }

    @GetMapping("/deleteCateById")
    public CustomResult deleteCate(int type,int id)
    {
        int count=categoryService.selectCateCount(type,id);
        if(count!=0)
        {
            return CustomResult.errorResult(500,"删除出错了").addResult("isDelete","no");
        }
        else if(categoryService.deleteCate(id))
        {
            return CustomResult.successResult().addResult("isDelete","yes");
        }
        return CustomResult.errorResult(500,"删除出错了").addResult("isDelete","no");
    }

    @GetMapping("/updateCategory")
    public CustomResult updateCategory(@RequestParam Map<String,Object> params)
    {
        if(categoryService.updateCategory(params))
        {
            return CustomResult.successResult().addResult("isUpdate","修改成功");
        }
        return CustomResult.errorResult(500,"删除出错了").addResult("isUpdate","修改失败");
    }

    @GetMapping("/selectTypeById")
    public CustomResult selectTypeById(int id)
    {
        return CustomResult.successResult().addResult("type",categoryService.selectTypeById(id));
    }
}
